﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Xml;
using System.IO;

namespace FITHOU_EDU_Server.xml
{
    public partial class XMLLichTheoLop : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fillCboLophoc();
            }
        }

        private void fillCboLophoc()
        {
            DataTable tbl = getAllClass();
            if (tbl.Rows.Count > 0)
            {
                foreach (DataRow row in tbl.Rows)
                {
                    ListItem li = new ListItem(row["sTenlop"].ToString(), row["PK_iLophocID"].ToString());
                    cboLophoc.Items.Add(li);
                }
            }
            tbl.Dispose();
        }

        protected void btnCreateXMLClass_Click(object sender, EventArgs e)
        {
            CalToXML();
            //int classId = int.Parse(cboLophoc.SelectedItem.Value); //Id lop hoc
            //String className = cboLophoc.SelectedItem.Text; //Ten lop hoc
            //String filePath = Server.MapPath("~/") + ConfigurationManager.AppSettings["xmlClassFilePath"].ToString();
            ////Response.Write("<script>alert('" + filePath + className + ".xml')</script>");
            ////return;
            //DataTable tbl = getAllCalendarByClassID(classId);
            //if (tbl.Rows.Count > 0)
            //{
            //    XmlDocument doc = new XmlDocument();
            //    XmlNode docNode = doc.CreateXmlDeclaration("1.0", "ASCII", null);
            //    doc.AppendChild(docNode);

            //    XmlNode FITHOU_EDU = doc.CreateElement("FITHOU-EDU-Calendar");
            //    doc.AppendChild(FITHOU_EDU);

            //    //XmlNode ClassName = doc.CreateElement("Class" + className);
            //    //FITHOU_EDU.AppendChild(ClassName);
            //    foreach (DataRow row in tbl.Rows)
            //    {
            //        XmlNode Calendar = doc.CreateElement("Calendar");
            //        DateTime dt = DateTime.Parse(row["dNgayhoc"].ToString());
            //        XmlNode NgayHoc = doc.CreateElement("Ngayhoc");
            //        NgayHoc.AppendChild(doc.CreateTextNode(String.Format("{0:dd/MM/yyyy}", dt)));
            //        Calendar.AppendChild(NgayHoc);

            //        XmlNode Giobatdau = doc.CreateElement("Giobatdau");
            //        Giobatdau.AppendChild(doc.CreateTextNode(row["sGiobatdau"].ToString()));
            //        Calendar.AppendChild(Giobatdau);

            //        XmlNode Gioketthuc = doc.CreateElement("Gioketthuc");
            //        Gioketthuc.AppendChild(doc.CreateTextNode(row["sGioketthuc"].ToString()));
            //        Calendar.AppendChild(Gioketthuc);

            //        String sTrangthai;
            //        sTrangthai = int.Parse(row["iTrangthai"].ToString()) == 0 ? "Nghỉ" : "Học";
            //        XmlNode iTrangthai = doc.CreateElement("Trangthai");
            //        iTrangthai.AppendChild(doc.CreateTextNode(sTrangthai));
            //        Calendar.AppendChild(iTrangthai);

            //        XmlNode sTenlop = doc.CreateElement("Tenlop");
            //        sTenlop.AppendChild(doc.CreateTextNode(row["sTenlop"].ToString()));
            //        Calendar.AppendChild(sTenlop);

            //        XmlNode sHotenGiangVien = doc.CreateElement("GiangVien");
            //        sHotenGiangVien.AppendChild(doc.CreateTextNode(row["sHoten"].ToString()));
            //        Calendar.AppendChild(sHotenGiangVien);

            //        XmlNode Phonghoc = doc.CreateElement("Phonghoc");
            //        Phonghoc.AppendChild(doc.CreateTextNode(row["sTenphong"].ToString()));
            //        Calendar.AppendChild(Phonghoc);

            //        XmlNode Monhoc = doc.CreateElement("Monhoc");
            //        Monhoc.AppendChild(doc.CreateTextNode(row["sTenmonhoc"].ToString()));
            //        Calendar.AppendChild(Monhoc);
            //        FITHOU_EDU.AppendChild(Calendar);
            //    }//End for
            //    doc.Save(filePath + className + ".xml");
            //    lblStatus.Text = "Tạo tệp " + className + ".xml thành công!";
            //}//End if
            //else
            //{
            //    lblStatus.Text = "Tạo tệp " + className + ".xml không thành công!";
            //}
            //tbl.Dispose();
        }


        public void CalToXML()
        {
            XmlTextWriter xwriter;

            int classId = int.Parse(cboLophoc.SelectedItem.Value); //Id lop hoc
            String className = cboLophoc.SelectedItem.Text; //Ten lop hoc
            String filePath = Server.MapPath("~/") + ConfigurationManager.AppSettings["xmlClassFilePath"].ToString();


            DataTable tbl = getAllCalendarByClassID(classId);
            if (tbl.Rows.Count > 0)
            {
                DateTime dt;
                String sTrangthai;
                xwriter = new XmlTextWriter(filePath + className + ".xml", System.Text.Encoding.Unicode);
                xwriter.WriteStartElement("FITHOU-EDU-Calendar", null); //root
                xwriter.WriteWhitespace("\n");
                foreach (DataRow row in tbl.Rows)
                {
                    xwriter.WriteWhitespace("\t");
                    xwriter.WriteStartElement("Calendar", DateTime.Now.ToShortDateString()); //start <Calendar>
                    xwriter.WriteWhitespace("\n\t\t");
                    //--------------------
                    dt = DateTime.Parse(row["dNgayhoc"].ToString());
                    xwriter.WriteElementString("Ngayhoc", DateTime.Now.ToShortDateString(), String.Format("{0:dd/MM/yyyy}", dt));
                    xwriter.WriteWhitespace("\n\t\t");
                    xwriter.WriteElementString("Giobatdau", "fithou.edu.net.vn", row["sGiobatdau"].ToString());
                    xwriter.WriteWhitespace("\n\t\t");
                    xwriter.WriteElementString("Gioketthuc", "fithou.edu.net.vn", row["sGioketthuc"].ToString());
                    xwriter.WriteWhitespace("\n\t\t");
                    sTrangthai = int.Parse(row["iTrangthai"].ToString()) == 0 ? "Nghỉ" : "Học";
                    xwriter.WriteElementString("Trangthai", "fithou.edu.net.vn", sTrangthai);
                    xwriter.WriteWhitespace("\n\t\t");
                    xwriter.WriteElementString("Tenlop", "fithou.edu.net.vn", row["sTenlop"].ToString());
                    xwriter.WriteWhitespace("\n\t\t");
                    xwriter.WriteElementString("GiangVien", "fithou.edu.net.vn", row["sHoten"].ToString());
                    xwriter.WriteWhitespace("\n\t\t");
                    xwriter.WriteElementString("Phonghoc", "fithou.edu.net.vn", row["sTenphong"].ToString());
                    xwriter.WriteWhitespace("\n\t\t");
                    xwriter.WriteElementString("Monhoc", "fithou.edu.net.vn", row["sTenmonhoc"].ToString());
                    xwriter.WriteWhitespace("\n\t");
                    //--------------------
                    xwriter.WriteEndElement(); //End </Calendar>
                    xwriter.WriteWhitespace("\n");
                }
                xwriter.WriteFullEndElement(); //end root
                xwriter.Close();
                lblStatus.Text = "Tạo tệp " + className + ".xml thành công!";
            }
            else
            {
                lblStatus.Text = "Tạo tệp " + className + ".xml không thất bại!";
            }
            tbl.Dispose();
        }


        private DataTable getAllClass()
        {
            String sqlCon = ConfigurationManager.ConnectionStrings["FITHOU_EDU"].ToString();
            SqlConnection Con = new SqlConnection(sqlCon);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adapter = null;
            DataTable tbl = new DataTable();
            try
            {
                Con.Open();
                cmd.CommandText = "sp_tblLophoc_GetAll";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = Con;
                adapter = new SqlDataAdapter(cmd);
                adapter.Fill(tbl);
            }
            catch (Exception e)
            {
                Response.Write(e.Message);
            }
            finally
            {
                if (Con.State != ConnectionState.Closed)
                    Con.Close();
                Con.Dispose();
            }
            return tbl;
        }//DataTable getAllClass

        private DataTable getAllCalendarByClassID(int classID)
        {
            String sqlCon = ConfigurationManager.ConnectionStrings["FITHOU_EDU"].ToString();
            SqlConnection Con = new SqlConnection(sqlCon);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adapter = null;
            DataTable tbl = new DataTable();
            try
            {
                Con.Open();
                cmd.CommandText = "sp_tblLichhoc_GetAllByClassID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = Con;
                cmd.Parameters.Add("@FK_iLophocID", SqlDbType.Int);
                cmd.Parameters[0].Value = classID;
                adapter = new SqlDataAdapter(cmd);
                adapter.Fill(tbl);
                //Response.Write("<script>alert('" + tbl.Rows.Count + "')</script>");
            }
            catch (Exception e)
            {
                Response.Write(e.Message);
            }
            finally
            {
                if (Con.State != ConnectionState.Closed)
                    Con.Close();
                Con.Dispose();
            }
            return tbl;
        }//DataTable getAllCadenlarByClassID
    }
}